<?php
/**
 * 汽车品牌
 * Created by PhpStorm.
 * User: andy
 * Date: 2019/11/19
 * Time: 下午6:42
 */

namespace app\common\model;

use think\Model;

class CarLabelModel extends Model
{
    const SHOW_STATUS = 1; //正常
    const HIDE_STATUS = 0; //禁用
    const DEL_STATUS = -1; //删除

    protected $autoWriteTimestamp = true;

    /**
     * 所有品牌
     * @param array $where
     * @param int $pageSum
     * @return \think\Paginator
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getALL($where = [], $pageSum = 0)
    {
        if ($pageSum <= 0) {
            $pageSum = getSystem("pageSUM") ? getSystem("pageSUM") : 10;
        }

        $list = self::where('status', '>', self::DEL_STATUS)
            ->where($where)
            ->order('list_order asc')
            ->paginate($pageSum);

        return $list;
    }


    /**
     * 删除
     * @param $id
     * @return array
     */
    public function del($id)
    {

        $au = self::get($id);
        $au->status = self::DEL_STATUS;

        $status = $au->save();
        if ($status) {
            return ['status' => 1, 'msg' => '删除成功！'];
        } else {
            return ['status' => 0, 'msg' => '删除失败！'];
        }
    }

    /**
     * 增加
     * @param $data
     * @return array
     * @throws \think\exception\DbException
     */
    public function adds($data)
    {
        //检测当前应用方法名是否重复
        if (!self::chackAction($data)) {
            return ['status' => 0, 'msg' => '标识已存在！'];
        }

        if (empty($data['id'])) {
            $status = self::save($data);

            if ($status) {
                return ['status' => 1, 'msg' => '增加成功！'];
            } else {
                return ['status' => 0, 'msg' => '增加失败！'];
            }
        } else {
            $status = self::save($data, ['id' => $data["id"]]);

            if ($status) {
                return ['status' => 1, 'msg' => '修改成功！'];
            } else {
                return ['status' => 0, 'msg' => '修改失败！'];
            }
        }

    }

    /**
     * 检测当前是否重复
     * @param $data
     * @return bool
     */
    private static function chackAction($data){
        if(empty($data['id'])){
            $data['id']=0;
        }

        $count=self::where('title',$data['title'])
            ->where('id',"<>",$data['id'])
            ->count();

        if($count>0){
            return false;
        }else{
            return true;
        }
    }

} 